package pe.org.cnl.congreso.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pe.org.cnl.congreso.dao.ParticipanteDAO;
import pe.org.cnl.congreso.model.FileUploadBean;
import pe.org.cnl.congreso.model.Participante;
import pe.org.cnl.congreso.service.ExcelService;
import pe.org.cnl.congreso.service.ParticipanteService;

@Controller
@Scope("session")
public class ExcelController {

	private static final Logger logger = Logger.getLogger(ExcelController.class);

	@Autowired
	private ExcelService excelService;
	@Autowired
	private ParticipanteService participanteService;
	
	@RequestMapping(value = "/uploadExcel.htm", method = RequestMethod.GET)
	public String uploadExcel(ModelMap model) {
		model.put("uploadItem", new FileUploadBean());
		return "carga";
	}
	
	@RequestMapping(value = "/uploadExcel2.htm", method = RequestMethod.GET)
	public String uploadExcel2(ModelMap model) {
		model.put("uploadItem", new FileUploadBean());
		return "carga2";
	}
	
	@RequestMapping(value = "/uploadExcel.htm", method = RequestMethod.POST)
	public String cargaExcel(FileUploadBean uploadItem,
			BindingResult result, HttpServletRequest request,
			HttpServletResponse response,ModelMap model)  {
		
		logger.debug("uploadarchivo ");
		int res =0;
		List<Participante> lParticipantes = null;
		
		try {
			
			lParticipantes = excelService.cargarParticipantes1( excelService.cargarExcel(uploadItem) );
			res = participanteService.registrarParticipantes( lParticipantes );
			logger.debug(" participantes en lista : " + lParticipantes.size() );
			logger.debug(" participantes registrados : " + res );
			model.put("mensaje", " Se obtubieron "+ lParticipantes.size() +" registros desde el Excel. Se han registrado a " + res + " asistentes. ");
		
		} catch (Exception e) {
			e.printStackTrace();
			model.put("msgError", "ERROR: "+ e.getMessage() );
		}finally{
			model.put("uploadItem", new FileUploadBean());	
		}
		return "carga";
	}


	@RequestMapping(value = "/uploadExcel2.htm", method = RequestMethod.POST)
	public String cargaExcel2(FileUploadBean uploadItem,
			BindingResult result, HttpServletRequest request,
			HttpServletResponse response,ModelMap model)  {
		
		logger.debug("uploadarchivo ");
		int res =0;
		List<Participante> lParticipantes = null;
		
		try {
			
			lParticipantes = excelService.cargarParticipantes2( excelService.cargarExcel(uploadItem) );
			res = participanteService.registrarParticipantes( lParticipantes );
			logger.debug(" participantes en lista : " + lParticipantes.size() );
			logger.debug(" participantes registrados : " + res );
			model.put("mensaje", " Se obtubieron "+ lParticipantes.size() +" registros desde el Excel. Se han registrado a " + res + " asistentes. ");
		
		} catch (Exception e) {
			e.printStackTrace();
			model.put("msgError", "ERROR: "+ e.getMessage() );
		}finally{
			model.put("uploadItem", new FileUploadBean());	
		}
		return "carga";
	}
	
}
